'use strict'
const path = require('path')
function resolve (dir) {
  return path.join(__dirname, dir)
}
const TerserPlugin = require('terser-webpack-plugin')

const name = '项目管理' // 浏览器标题

// 前端代码打包后部署的路径
const publicPath = {
  development: '/',
  staging: '/',
  production: '/test'
}[process.env.NODE_ENV]

module.exports = {
  lintOnSave: false,
  productionSourceMap: false,
  publicPath: './',
  devServer: {
    port: 9090,
    open: true,
    proxy: {
      '/apis': {
        target: 'http://192.168.8.181:925',
        changeOrigin: true,
        ws: false,
        pathRewrite: {
          '^/apis': ''
        }
      },
      '^/oapi': {
        target: 'https://oapi.dingtalk.com',
        changeOrigin: true,
        pathRewrite: {
          '^/oapi': ''
        }
      },
      '^/dingtalk': {
        target: 'https://api.dingtalk.com',
        changeOrigin: true,
        pathRewrite: {
          '^/dingtalk': ''
        }
      }
    }
  },
  css: {
    loaderOptions: {
      scss: {
        additionalData: '@import "@/styles/variable.scss";'
      }
    }
  },
  configureWebpack: {
    name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  chainWebpack (config) {
    // set svg-sprite-loader
    config.module.rule('svg').exclude.add(resolve('src/base/icon')).end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/base/icon'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
    config.when(process.env.NODE_ENV !== 'development', config => {
      config
        .plugin('ScriptExtHtmlWebpackPlugin')
        .after('html')
        .use('script-ext-html-webpack-plugin', [
          {
            inline: /runtime\..*\.js$/
          }
        ])
        .end()
      config.optimization
        .splitChunks({
          chunks: 'all',
          cacheGroups: {
            libs: {
              name: 'chunk-libs',
              test: /[\\/]node_modules[\\/]/,
              priority: 10,
              chunks: 'initial'
            },
            elementUI: {
              name: 'chunk-elementUI',
              priority: 20,
              test: /[\\/]node_modules[\\/]_?element-ui(.*)/
            },
            commons: {
              name: 'chunk-commons',
              test: resolve('src/components'),
              minChunks: 3,
              priority: 5,
              reuseExistingChunk: true
            }
          }
        })
        .minimizer('alljs')
        .use(TerserPlugin, [
          {
            test: /\.js(\?.*)?$/i,
            terserOptions: {
              compress: {
                drop_console: true
              }
            }
          }
        ])
    })
  }
}